﻿/*****************************************************************************//*!
* file  : filex test.c
* creat : apleilx
* data  : 2021/02/15
*******************************************************************************/
#include "fx_api.h"
#include "os_obj.h"
#include "bsp_spim.h"
#include "common.h"
#include "bsp_hal.h"
#include "sys_task.h"

#ifdef FX_ENABLE_FAULT_TOLERANT
#include "fx_fault_tolerant.h"
#endif

#define DEMO_STACK_SIZE 		2048
#define MEDIA_BUFF_SIZE			4096

//读写测试
#define SPIM_FILE_W_TST_EN    0
#define SPIM_FILE_R_TST_EN    1

//如果外部操作SPIM破坏了扇区信息，需要擦除重建文件系统
#define SPIM_FAT_CLR          0

/******************************************************************************
*	                            var
*******************************************************************************/

#ifdef FX_ENABLE_FAULT_TOLERANT
UCHAR         fault_tolerant_memory[FX_FAULT_TOLERANT_MAXIMUM_LOG_FILE_SIZE];
#endif

//Define what the initial system looks like.
uint64_t    stk_task_filex[DEMO_STACK_SIZE / 8];

/******************************************************************************
*	                            function
*******************************************************************************/
VOID	_fx_spim_driver(FX_MEDIA *media_ptr);
void	filex_task_entry(ULONG thread_input);
void	spim_file_test(void);

/*******************************************************************************
* @brief  tx_filex_application_define
* \param[in] none
* \retval: none
*******************************************************************************/
void    tx_filex_application_define(void)
{

    CHAR *pointer;

    /* Put first available memory address into a character pointer.  */
    pointer = (CHAR *)stk_task_filex;

    /* Create the file opt thread.  */
    tx_thread_create(&os_obj.tcb_task_filex,
        "filex test",
        filex_task_entry,
        0,
        pointer,
        DEMO_STACK_SIZE,
        APP_CFG_TASK_FILEX_PRIO,
        APP_CFG_TASK_FILEX_PRIO,
        TX_NO_TIME_SLICE,
        TX_DONT_START);

    os_obj.tid_filex = &os_obj.tcb_task_filex;

    /* Initialize FileX.  */
    fx_system_initialize();

    FLASH_InitExtFlash();

    //program to spim
#if SPIM_FAT_CLR > 0
    FLASH_UnlockBank3();

    FLASH_ErasePage(0x8400000);
    FLASH_ErasePage(0x8400000 + 1 * 4096);
    FLASH_ErasePage(0x8400000 + 2 * 4096);
    FLASH_ErasePage(0x8400000 + 3 * 4096);
    FLASH_ErasePage(0x8400000 + 4 * 4096);
    FLASH_ErasePage(0x8400000 + 5 * 4096);
    FLASH_ErasePage(0x8400000 + 6 * 4096);
    FLASH_ErasePage(0x8400000 + 7 * 4096);
    FLASH_ErasePage(0x8400000 + 8 * 4096);
    FLASH_ErasePage(0x8400000 + 9 * 4096);
#endif

    tx_thread_resume(os_obj.tid_filex);
}


/*******************************************************************************
* @brief  filex_task_entry
* \param[in] thread_input : argv
* \retval: none
*******************************************************************************/
UINT  _lx_nor_flash_hal_erase_all(VOID);
void    filex_task_entry(ULONG thread_input)
{
    osDelay(5);

    spim_file_test();

    //tx_thread_suspend(os_obj.tid_filex);
    while (1)
    {
        osDelay(ms_ticks(100));
        
        if(sys_st.lx_clr_req)
        {
            file_dbg_print("LX CLR START\n");
            sys_st.lx_clr_req = 0;
            //_lx_nor_flash_hal_erase_all();
            file_dbg_print("LX CLR END\n");
        }
        
        if(sys_st.lx_tst_req)
        {
            file_dbg_print("LX TEST START\n");
            sys_st.lx_tst_req = 0;
            //lx_file_test();
            file_dbg_print("LX TEST END\n");
        }
    }
}

/*******************************************************************************
* @brief  spim_file_test
* \param[in] none
* \retval: none
*******************************************************************************/
void spim_file_test(void)
{
    UINT  status;
    ULONG actual;
    CHAR  local_buffer[64];
	unsigned long *spim_media_memory = 0;
	FX_MEDIA *spim_disk = 0;
	FX_FILE *spim_file = 0;
	
	do
	{
		osDelay(10);
		spim_media_memory = malloc(MEDIA_BUFF_SIZE+16);
	} while(!spim_media_memory);
	
	do
	{
		osDelay(10);
		spim_disk = malloc(sizeof(FX_MEDIA)+16);
	} while(!spim_disk);
	
	do
	{
		osDelay(10);
		spim_file = malloc(sizeof(FX_FILE)+16);
	} while(!spim_file);
	
    //open spim disk
    status = fx_media_open(spim_disk,
        "SPIM DISK",
        _fx_spim_driver,
        0,
        spim_media_memory,
        MEDIA_BUFF_SIZE);


    /* Check the media open status.  */
    if (status != FX_SUCCESS)
    {
        status = fx_media_format(spim_disk,
            _fx_spim_driver,              // Driver entry
            0,                            // RAM disk memory pointer
            (uu8*)spim_media_memory,      // Media buffer pointer
            MEDIA_BUFF_SIZE,			  // Media buffer size
            "SPIM",                       // Volume Name
            1,                            // Number of FATs
            32,                           // Directory Entries
            0,                            // Hidden sectors
            8 * 1024 * 1024 / 4096,       // Total sectors
            4096,                         // Sector size
            1,                            // Sectors per cluster
            1,                            // Heads
            1);                           // Sectors per track

        //open spim disk
        status = fx_media_open(spim_disk,
            "SPIM DISK",
            _fx_spim_driver,
            0,
            spim_media_memory,
            MEDIA_BUFF_SIZE);
    }

    do
    {
        if (status != FX_SUCCESS)
        {
            break;
        }

        /* Create a file called TEST.TXT in the root directory.  */
        status = fx_file_create(spim_disk, "SPIM.TXT");

        /* Check the create status.  */
        if (status != FX_SUCCESS)
        {

            /* Check for an already created status. This is expected on the
               second pass of this loop!  */
            if (status != FX_ALREADY_CREATED)
            {
                /* Create error, break the loop.  */
                file_dbg_print("SPIM.TXT creat: ERROR\n");
                break;
            }
            else
            {
                file_dbg_print("SPIM.TXT creat: FX_ALREADY_CREATED\n");
            }
        }
        else
        {
            file_dbg_print("SPIM.TXT creat: OK\n");
        }

        /* Open the test file.  */
        status = fx_file_open(spim_disk, spim_file, "SPIM.TXT", FX_OPEN_FOR_WRITE);

        /* Check the file open status.  */
        if (status != FX_SUCCESS)
        {

            /* Error opening file, break the loop.  */
            file_dbg_print("SPIM.TXT OPEN: ERROR\n");
            break;
        }
        else
        {
            file_dbg_print("SPIM.TXT OPEN: OK, File Size: %d B\n\n", (uu32)spim_file.fx_file_current_file_size);
        }

        /* Seek to the beginning of the test file.  */
        status = fx_file_seek(spim_file, 128 * 1024);

        /* Check the file seek status.  */
        if (status != FX_SUCCESS)
        {

            /* Error performing file seek, break the loop.  */
            file_dbg_print("SPIM.TXT SEEK: ERROR\n");
            break;
        }

        /* Write a string to the test file.  */
#if SPIM_FILE_W_TST_EN > 0
        status = fx_file_write(spim_file, "\n123 status = fx_file_write        \n", 32);
        status = fx_file_write(spim_file, "\n456 writing , break the loop      \n", 32);
        status = fx_file_write(spim_file, "\n789 fx_file_read(spim_file,      \n", 32);

        /* Check the file write status.  */
        if (status != FX_SUCCESS)
        {
            file_dbg_print("SPIM.TXT WRITE: ERROR\n");
            /* Error writing to a file, break the loop.  */
            break;
        }
#endif

        /* Seek to the beginning of the test file.  */
        status = fx_file_seek(spim_file, 0);

        /* Check the file seek status.  */
        if (status != FX_SUCCESS)
        {
            file_dbg_print("SPIM.TXT SEEK AFTER WRITE: ERROR\n");
            /* Error performing file seek, break the loop.  */
            break;
        }

#if SPIM_FILE_R_TST_EN > 0
        /* Read the first 28 bytes of the test file.  */
        file_dbg_print("SPIM.TXT READ DAT :\n");
        while (1)
        {
            status = fx_file_read(spim_file, local_buffer, 60, &actual);
            local_buffer[actual] = 0;

            /* Check the file read status.  */
            if (status != FX_SUCCESS)
            {
                if (status != FX_END_OF_FILE)
                {
                    file_dbg_print("SPIM.TXT READ: ERROR -%d\n", status);
                    /* Error reading file, break the loop.  */
                    break;
                }
                else
                {
                    file_dbg_print("%s", local_buffer);
                    break;
                }
            }

            file_dbg_print("%s", local_buffer);

            osDelay(ms_ticks(10));
        }
        file_dbg_print("\n\nSPIM FILE READ END!\n\n");

        /* Check the file read status.  */
        if ((status != FX_SUCCESS) && (status != FX_END_OF_FILE))
        {
            break;
        }
#endif

        /* Close the test file.  */
        status = fx_file_close(spim_file);

        /* Check the file close status.  */
        if (status != FX_SUCCESS)
        {
            /* Error closing the file, break the loop.  */
            break;
        }

    } while (0);

    fx_media_close(spim_disk);
	
	free(spim_media_memory);
	free(spim_file);
	free(spim_disk);
}
